package com.jlastu.dlxy_androidapp.view.main;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.entity.Product1;
import com.jlastu.dlxy_androidapp.adapter.ProductAdapter;
import com.jlastu.dlxy_androidapp.view.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCartActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ProductAdapter adapter;
    private List<Product1> products;
    @Override
    public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 检查屏幕方向是否为横屏
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Orientation", "Landscape detected");

            // 创建并启动新的Activity
            Intent intent = new Intent(this, LandscapeActivity.class);
            startActivity(intent);

            // 可选：关闭当前Activity
            finish();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        TextView toolbar = findViewById(R.id.toolbar);


        Button manageButton = findViewById(R.id.manageButton);
        manageButton.setOnClickListener(view -> {
            // 管理按钮的点击处理逻辑

                if (adapter.isInEditMode()) {
                    // 退出编辑模式
                    adapter.setInEditMode(false);
                    adapter.notifyDataSetChanged();
                    manageButton.setText("管理");
                    // 其他相关操作，如显示合计金额等
                } else {
                    // 进入编辑模式
                    adapter.setInEditMode(true);
                    adapter.notifyDataSetChanged();
                    manageButton.setText("完成");
                    // 其他相关操作，如隐藏合计金额等
                }

        });

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        products = new ArrayList<>();

//         //从首页商品点击购物车按钮后，将商品添加到products列表中
//        private void addToCart(Product1 product) {
//           products.add(product);
//        // 刷新RecyclerView以显示新添加的商品
//         if (recyclerView.getAdapter() == null) {
//               ProductAdapter adapter = new ProductAdapter(products);
//              recyclerView.setAdapter(adapter);
//          } else {
//                recyclerView.getAdapter().notifyItemInserted(products.size() - 1);
//           }
//        }

        adapter = new ProductAdapter(products);
        recyclerView.setAdapter(adapter);

        CheckBox selectAllCheckBox = findViewById(R.id.selectAllCheckBox);
        // 全选框的逻辑处理

        selectAllCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            for (Product1 product : products) {
                product.setSelected(isChecked);
            }
            adapter.notifyDataSetChanged();
        });

        TextView totalTextView = findViewById(R.id.totalTextView);
        // 设置合计金额

        double totalAmount = 0;
        for (Product1 product : products) {
            if (product.isSelected()) {
                totalAmount += product.getPrice() * product.getQuantity();
            }
        }
        totalTextView.setText("合计：" + totalAmount);



        Button checkoutButton = findViewById(R.id.checkoutButton);
        checkoutButton.setOnClickListener(view -> {
            // 结算按钮的点击处理逻辑

            int totalAmount1 = 0;
            List<Product1> selectedProducts = new ArrayList<>();
            for (Product1 product : products) {
                if (product.isSelected()) {
                    totalAmount1 += product.getPrice() * product.getQuantity();
                    selectedProducts.add(product);
                }
            }

            // 执行结算操作,跳转到支付页面等

        });

        //返回按钮的实现
        ImageView mback =findViewById(R.id.back);
        mback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 创建一个 Intent，指定目标为包含 MineFragment 的 Activity
                Intent intent = new Intent(ShoppingCartActivity.this, MainActivity.class);
                // 添加标志位，指示要返回到 MineFragment
                intent.putExtra("fragment_mine", "MineFragment");
                // 启动 Intent
                startActivity(intent);
                // 结束当前 Activity
                finish();
            }
        });
    }
}
